package com.guoguo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.StaticResource;
import com.guoguo.entity.*;
import com.guoguo.enums.PayEnum;
import com.guoguo.mapper.MoneyFlowMapper;
import com.guoguo.mapper.OrdersMapper;
import com.guoguo.mapper.PointFlowMapper;
import com.guoguo.mapper.UserExtMapper;
import com.guoguo.service.IPayService;
import com.guoguo.utils.DateUtils;
import com.guoguo.utils.RedisUtils;
import com.guoguo.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/13 0013.
 */

     /*
                           _ooOoo_
                          o8888888o
                          88" . "88
                          (| -_- |)
                          O\  =  /O
                       ____/`---'\____
                     .'  \\|     |//  `.
                    /  \\|||  :  |||//  \
                   /  _||||| -:- |||||-  \
                   |   | \\\  -  /// |   |
                   | \_|  ''\---/''  |   |
                   \  .-\__  `-`  ___/-. /
                 ___`. .'  /--.--\  `. . __
              ."" '<  `.___\_<|>_/___.'  >'"".
             | | :  `- \`.;`\ _ /`;.`/ - ` : | |
             \  \ `-.   \_ __\ /__ _/   .-` /  /
        ======`-.____`-.___\_____/___.-`____.-'======
                           `=---='
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 佛祖保佑       永无BUG
        */
@Service
public class PayServiceImpl implements IPayService {

    //UserExtr接口
    @Autowired
    private UserExtMapper userExtMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private MoneyFlowMapper moneyFlowMapper;

    @Autowired
    private PointFlowMapper pointFlowMapper;

    public Orders createOrder(int payType, String userId) {
        //创建订单
        Orders orders = new Orders();
        orders.setOrderNo(UUIDUtils.getOrderNO());
        orders.setUserId(userId);
        orders.setCreateTime(new Date());
        orders.setUpdateTime(new Date());
        orders.setOrderType(payType);
        int num = ordersMapper.insert(orders);
        if (num > 0) {
            return orders;
        } else {
            return new Orders();
        }
    }

    /**
     * 订单明细
     *
     * @param payType
     * @param orderNo
     * @param albId
     * @param price
     * @return
     */
    @Override
    public int createPayJournal(int payType, String orderNo, String albId, String price) {
        int num = 0;
        if (payType == 1 || payType == 3) {
            PointFlow pointFlow = new PointFlow();
            pointFlow.setOrderNo(orderNo);
            pointFlow.setMoney(price);
            //1：充值；2：消费
            pointFlow.setPaymentType(payType == 1 ? 1 : 2);
            pointFlow.setAlbumId(albId);
            num = pointFlowMapper.insertSelective(pointFlow);
        }
        if (payType == 2 || payType == 4) {
            MoneyFlow moneyFlow = new MoneyFlow();
            moneyFlow.setOrderNo(orderNo);
            moneyFlow.setMoney(price);
            //1：充值；2：消费
            moneyFlow.setPaymentType(payType == 2 ? 1 : 2);
            moneyFlow.setAlbumId(albId);
            num = moneyFlowMapper.insertSelective(moneyFlow);
        }

        return num;
    }

    /**
     * 支付回调方法
     *
     * @param rechargeId
     * @param payType
     * @param userExt
     */
    public void payCallBack(String rechargeId, int payType, UserExt userExt) {
        RedisUtils redisUtils = RedisUtils.getInstance();
        String payTypeMap = redisUtils.hget(StaticResource.SYS_INIT_RECHARGE_KEY, rechargeId);
        JSONObject rechargeJson = JSONObject.parseObject(payTypeMap);
        if (payType == PayEnum.PAY_TYPE_RECHARGE.getId()) {
            userExt.setPoint(userExt.getPoint() + rechargeJson.getIntValue("point"));
            //修改用户积分
            userExtMapper.insertUserExt(userExt);
            //订单类型
//            orders.setOrderType(0);
        }
        if (payType == PayEnum.PAY_TYPE_MEMBER.getId()) {
            userExt.setIsMember(true);

        }
    }

    @Override
    public List<Orders> getOrderList(String userId) {
        return ordersMapper.getOrderList(userId);
    }


}
